package com.itheima.service.impl;
import com.itheima.dto.LinechartDto;
import com.itheima.mapper.LinechartMapper;
import com.itheima.service.LinechartService;
import org.apache.dubbo.config.annotation.DubboService;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
/**
 * @author YuanDao
 * @date 2021/3/26 18:52
 */
@DubboService
public class LinechartServiceImpl implements LinechartService {
    @Resource
    private LinechartMapper linechartMapper;
    //会员人数统计
    @Override
    public LinechartDto lineChat(String startDate, String closeDate) {
        //判断传来数据是否存在
        if (startDate != null && closeDate != null) {
            //转换日期格式
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            Date StartDate = new Date(startDate);
            Date endDate = new Date(closeDate);
            String StartStr = sdf.format(StartDate);
            String endStr = sdf.format(endDate);

            //将查询对象存入dto
            List<LinechartDto> dtoList = linechartMapper.lineChat(StartStr, endStr);
            LinechartDto linechartDto = new LinechartDto();
            List<Long> longs = new ArrayList<>();
            List<String> dateList = new ArrayList<>();

            //判断dto是否为空
            if (dtoList != null) {
                dtoList.forEach(sjkList ->{

                    //获取人数
                    longs.add(sjkList.getCount());

                    //获取日期list
                    String substring = sjkList.getFdate().substring(0,7);

                    //根据索引截取字符串
                    dateList.add(substring);
                });
                linechartDto.setCounts(longs);
                linechartDto.setFdates(dateList);
            }
            return linechartDto;
        }
        return null;
    }
    //会员性别统计
    @Override
    public List<Map> selectReportSex() {
        return linechartMapper.selectReportSex();
    }
}
